package org.crystal.jqweb.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crystal.jqweb.config.JqWebProperties;

/**
 * Filter
 * 
 * @author 107
 */
public class JqWebCharsetEncodingFilter implements Filter {
	private final Log log = LogFactory.getLog(getClass());
	private String Encoding = "";

	public void init(FilterConfig config) throws ServletException {

		String encoding=JqWebProperties.getInstance().getValue("jqweb.charset.encoding");
		
		if(Encoding!=null){
			Encoding=encoding;
		}
		
		log.debug("init() Encoding:"+Encoding);
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		//String type = request.getContentType();
		HttpServletRequest hsrequest = (HttpServletRequest)request;
		hsrequest.setCharacterEncoding(Encoding);
		log.debug("doFilter() Encoding:"+Encoding);
		chain.doFilter(hsrequest, response);

	}

	public void destroy() {
	}

}
